package com.ngkj.zmfptback.sys.initData.servicie.read.impl;

import com.ngkj.zmfptCommon.common.util.TreeUtil;
import com.ngkj.zmfptback.sys.initData.bean.SysArea;
import com.ngkj.zmfptback.sys.initData.mapper.read.AreaMapper;
import com.ngkj.zmfptback.sys.initData.servicie.read.AreaService;
import com.ngkj.zmfptback.sys.initData.util.AreaUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/11/1 creation
 **/
@Service
@Transactional(readOnly = true)
public class AreaServiceImlp implements AreaService {

    @Resource
    private AreaMapper areaMapper;



    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Map<String, List<SysArea>> getAllAreas() {
        List<SysArea> allAreas = areaMapper.getAllAreas();
        return AreaUtils.getStringListMap(allAreas);
    }

    @Override
    public List<SysArea> getSelectAreas(String pid) {
        String cacheName = "appAreaCache."+pid;
        List<SysArea> sysAreaList= (List<SysArea>) redisTemplate.opsForValue().get(cacheName);
        return sysAreaList;
    }

    /**
     * 以地区首个大写字母分组
     * @return
     */
    @Override
    public Map<String, List<SysArea>> getGroupFirst() {
        List<SysArea> allAreas = areaMapper.getAllAreas();
        Map<String,List<SysArea>> listMap = new HashMap<>();
        allAreas.stream().forEach(item->{
            String firstLetter = item.getFirstLetter();
            if (item.getLevel()==2){
                if (listMap.containsKey(firstLetter)) {
                    List<SysArea> sysAreas = listMap.get(firstLetter);
                    sysAreas.add(item);
                    listMap.put(firstLetter,sysAreas);
                }else{
                    List<SysArea> areaList = new ArrayList<>();
                    areaList.add(item);
                    listMap.put(firstLetter,areaList);
                }
            }

        });
        return listMap;
    }

    @Override
    public Map<String, List<SysArea>> getGroupFirstFromCache() {
        String cacheKeyName = "areaFirstGroup";
        Map<String, List<SysArea>> o = (Map<String, List<SysArea>>)redisTemplate.opsForValue().get(cacheKeyName);
        return o;
    }

    @Override
    public List<Map<String, Object>> getAllAreas2Tree(SysArea sysArea) {
        List<Map<String, Object>> allAreaToMap = areaMapper.getAllAreaToMap(sysArea);
        List<Map<String, Object>> tree = TreeUtil.getTree(allAreaToMap, "areaId", "parentId");
        return tree;
    }

    @Override
    public Map<String, Object> getAreaById(String id) {
        return areaMapper.getAreaById(id);
    }

    @Override
    public Long getAreaIdByAreaCode(String areaCode) {
        return areaMapper.getAreaIdByAreaCode(areaCode);
    }


}
